package com.cwh.taskcenter.task.gateway;

import com.cwh.taskcenter.domain.task.gateway.UserTaskRecordGateWay;
import com.cwh.taskcenter.domain.task.model.UserTaskRecord;
import com.cwh.taskcenter.task.converter.UserTaskRecordConverter;
import com.cwh.taskcenter.task.mapper.UserTaskRecordMapper;
import com.cwh.taskcenter.task.model.UserTaskRecordDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cwh
 */
@Component
public class UserTaskRecordGateWayImpl implements UserTaskRecordGateWay {

    @Autowired
    private UserTaskRecordMapper userTaskRecordMapper;


    @Override
    public void save(UserTaskRecord userTaskRecord) {
        UserTaskRecordDO userTaskRecordDO = UserTaskRecordConverter.toUserTaskRecordDO(userTaskRecord);
        userTaskRecordMapper.create(userTaskRecordDO);
    }

    @Override
    public void batchSave(List<UserTaskRecord> userTaskRecords) {
        if (CollectionUtils.isEmpty(userTaskRecords)) {
            return;
        }
        List<UserTaskRecordDO> userTaskRecordDOList = userTaskRecords.stream().map(UserTaskRecordConverter::toUserTaskRecordDO).collect(Collectors.toList());
        userTaskRecordMapper.creates(userTaskRecordDOList);
    }

    @Override
    public boolean existsByUniqueKey(String unionKey) {
        long count = userTaskRecordMapper.existsByUniqueKey(unionKey);
        return count > 0;
    }

    @Override
    public List<LocalDate> getUserSignInDates(String eventType, Long userId, Long taskId) {
        return userTaskRecordMapper.getUserSignInDates(eventType, userId,taskId);
    }
}
